package com.sskj.b70.v.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.header.MaterialHeader;
import com.sskj.b70.R;
import com.sskj.b70.adapter.slimadapter.IViewInjector;
import com.sskj.b70.adapter.slimadapter.SlimAdapter;
import com.sskj.b70.adapter.slimadapter.SlimInjector;
import com.sskj.b70.common.App;
import com.sskj.b70.common.decoration.DividerLineItemDecoration;
import com.sskj.b70.m.bean.DagBean;
import com.sskj.b70.mvchelper.ModelRx2DataSource;
import com.sskj.b70.p.fragment.DagListFragmentPresenter;
import com.sskj.b70.v.activity.DagListActivity;
import com.sskj.b70.v.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-10-11 17:12
 */
public class DagListFragment extends BaseFragment<DagListActivity, DagListFragmentPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    Unbinder unbinder;
    private SlimAdapter<DagBean> slimAdapter;
    private int type;

    public static DagListFragment newInstance(int type) {

        Bundle args = new Bundle();
        args.putInt("type", type);
        DagListFragment fragment = new DagListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_dag_list;
    }

    @Override
    public DagListFragmentPresenter getPresenter() {
        return new DagListFragmentPresenter();
    }

    @Override
    public void initView() {
        type = getArguments().getInt("type", 0);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setRightPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 30))
                .setLeftPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 30))
                .setLastDraw(false)
                .setPaintColor(ContextCompat.getColor(App.INSTANCE, R.color.dag_line_color))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.recy_item_dag, new SlimInjector<DagBean>() {
            @Override
            public void onInject(DagBean data, IViewInjector injector) {
                injector.text(R.id.tvTime, data.getClearTime())
                        .text(R.id.tvWeekMinNum, data.getStockUserFund())
                        .text(R.id.tvMinTitle, String.format(getString(R.string.week_min_num),data.getStockCode()))
                        .text(R.id.tvShareNumTitle, String.format(getString(R.string.week_share_num),data.getStockCode()))
                        .text(R.id.tvShareNum, data.getShareNum())
                        .text(R.id.tvStatus, !data.isRelease() ? getString(R.string.no_send) : getString(R.string.has_send));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<DagBean> dataSource =
                new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<DagBean>)
                        page -> mPresenter.getList(page, type));
        coolRefreshView.setPullHeader(new MaterialHeader(getActivity()));
        MVCCoolHelper<List<DagBean>> mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcCoolHelper.setDataSource(dataSource);
        mvcCoolHelper.setAdapter(slimAdapter);
        mvcCoolHelper.refresh();
    }

}
